package yh.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import yh.core.util.db.YHDBUtility;

public class YHMTToFlowFUtil {
	  public  static String configPath = "/core/funcs/workflow/workflowUtility/managementT_formT_config.properties";
	  public  static  Properties p = null;
	//{"KEY":"主键","PRO_ID":"物品ID","PRO_NAME":"物品名称","TRANS_QTY":"领用数量","REMARK":"备注","BORROWER":"领用人","BORROWER_ID":"领用人ID"}
	  public static void main(String []args){
//		 Map a=new HashMap();
//		 a.put("KEY", "1111");
//		 a.put("PRO_ID", "2222");
//		 a.put("TRANS_QTY", "3333");
//		 a.put("REMARK", "备注");
//		 a.put("PRO_NAME", "物品名称");
//		 Map arrayMap =mdataToFormData(a);
//		 Set s=arrayMap.keySet();
//		 Iterator i=s.iterator();
//		 while(i.hasNext()){
//			 String title=i.next().toString();
//			 System.out.println(title+":"+arrayMap.get(title));
//		 }   
	 }
	  
	 public static void deal(HttpServletRequest request ){
		
	 }
	  public static Map mdataToFormData(HttpServletRequest request,int formid,String flowName,Connection dbConn){
		  configPath=request.getRealPath("/")+configPath;
		  Map arrayData=new HashMap();
		  String map = getKeyValue(flowName).trim();
	        String[] maps = map.split(",");
	        for (String s : maps) {
	          if (!"".equals(s)) {
	            String[] keyValue = s.split(":");
	            String dv=request.getParameter(keyValue[0]);
	            if (dv!=null) {
//	              String dataidKey=formTitleToDataID(formid,keyValue[1],dbConn);
	              arrayData.put(keyValue[1], request.getParameter(keyValue[0]));
	            }
	          }
	        }
	        return arrayData;
	  }
	  public static String formTitleToDataID(int formid,String title,Connection dbConn){
		  String query = "select TITLE , NAME from oa_fl_form_item  where  FORM_ID="+formid+" and TITLE='"+title+"'";
	      Statement stm1 = null;
	      ResultSet rs1 = null;
	      String dName ="";
	      try {
	        stm1 = dbConn.createStatement();
	        rs1 = stm1.executeQuery(query);
	        if (rs1.next()){
	           dName = rs1.getString("NAME");
	        
	        }
	      } catch(Exception ex) {
	         ex.printStackTrace();
	      } finally {
	        YHDBUtility.close(stm1, rs1, null); 
	      }
	      return dName;
	  }
	  public static String getUserIds(String query,Connection dbConn){
		  
	      Statement stm1 = null;
	      ResultSet rs1 = null;
	      StringBuffer dName =new StringBuffer();
	      try {
	        stm1 = dbConn.createStatement();
	        rs1 = stm1.executeQuery(query);
	        while (rs1.next()){
	           dName .append( rs1.getString("seq_id")+",");
	        
	        }
	      } catch(Exception ex) {
	         ex.printStackTrace();
	      } finally {
	        YHDBUtility.close(stm1, rs1, null); 
	      }
	      return dName.toString();
	  }
	  public static String getKeyValue(String flowNameKey){
		  if(p==null)
			try {
				p=new Properties();
				p.load(new InputStreamReader(new FileInputStream(new File(configPath)) , "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		  System.out.println(p.get("office_product_draw"));
		  
		  return (String) p.get(flowNameKey);
	  }
}
